<?php

namespace App\Http\Controllers\Firm;

use App\Http\Controllers\BaseController;
use App\Models\FirmProfile;
use Illuminate\Http\Request;

class ProfileController extends BaseController
{
    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'firm_id' => 'integer',
            'banners' => 'array',
            'video_url' => 'string',
            'is_face' => 'in:0,1',
            'invest_amount' => 'required_if:is_face,0',
            'join_in_fee' => 'string',
            'store_num' => 'integer',
            'set_up_time' => 'string',
            'cooperation_area' => 'array',
            'manage_method' => 'string',
            'invest_crowd' => 'string',
            'tags' => 'array',
            'cover_one' => 'string',
            'cover_two' => 'string',
            'intro' => 'string',
            'advantage' => 'string',
            'contact' => 'array'
        ]);

        $exist = FirmProfile::query()->where('firm_id',$param['firm_id'])->exists();

        if ($exist){
            $res = FirmProfile::query()->where('firm_id',$param['firm_id'])->update($param);

            return  $res ? $this->success() : $this->error();
        }

        $profile = FirmProfile::query()->create($param);


        if ($profile->getKey()){
            event(new \App\Events\FirmProfile(auth('api')->id()));
            return $this->success();
        }

        return  $this->error();
    }
}
